<?php

require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_USERS;
require_once LIBRARIES_ENTITIES_GROUPS;
require_once LIBRARIES_ENTITIES_FUNCTIONS;
require_once LIBRARIES_ENTITIES_GROUPUSERS;
require_once LIBRARIES_ENTITIES_GROUPFUNCTIONS;

require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;

class LoginController {

    public function invoke() {

        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        } else if (isset($_GET['action'])) {
            $action = $_GET['action'];
        }

        switch ($action) {
            case 'login':

                // user name
                $userName = $_POST['txtUserName'];

                // user object
                $user = $this->getUserByUserName($userName);

                if ($user != null) {

                    if ($user->getStatus() == 1) {
                        // password
                        $password = md5($_POST["txtPassword"]);

                        // check password
                        if ($password == $user->getPassword()) {

                            // get group list of user
                            $groupUserList = $this->getGroupUserListByUserID($user->getID());


                            $_SESSION["session"] = array(
                                "sUserName" => $user->getUserName(),
                                "sFullname" => $user->getFullname(),
                                "sUserID" => $user->getID(),
                                "sGroupID" => $groupUserList[0]->getGroupID()
                            );

                            echo 'OK';
                        } else {
                            echo 'Sai thông tin đăng nhập, vui lòng thử lại';
                        }
                    }else{
                        echo 'Tài khoản chưa được kích hoạt, vui lòng liên hệ quản trị';
                    }
                } else {
                    echo 'Sai thông tin đăng nhập, vui lòng thử lại';
                }
                break;
            case 'logout':
                Utility::destroySession();
                Utility::redirect('.');
                break;
            default:
                $xtemplate = new XTemplate($dir . '/views/login.html');
                $xtemplate->parse('main');
                $xtemplate->out('main');
                break;
        }
    }

    // get user detail by username
    private function getUserByUserName($userName) {
        $db = new Database();
        try {
            $user = new Users();
            $user->setUserName($userName);
            $user->getByUserName($db);
            $db->submitChanges();
            return $user;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // get functions list by groupID
    private function getGroupFunctionsByGroupID($groupID) {
        $db = new Database();
        try {
            $groupFunction = new GroupFunctions();
            $groupFunction->setGroupID($groupID);
            $rows = $groupFunction->getListByGroupID($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // get group list by userID
    private function getGroupUserListByUserID($userID) {
        $db = new Database();
        try {
            $groupUser = new GroupUsers();
            $groupUser->setUserID($userID);
            $rows = $groupUser->getListByUserID($db);
            $db->submitChanges();
            return $rows;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
